/**
 * @author ccpalu
 *	Functions for text file details, that the user must inform when using mannual uploading.
 */
function configOptions(){
/* Creates a box with graph configuration parameters */
	var parent = configDiv();
	var divTitle = createParagraph('graphConfigP', 'Configurations', parent);
	setPosition(divTitle, 155, 45);
	setFont(divTitle,'18px',0,0,0);
	
	createConfigForm();

	return false;
}

function configDiv(){
/* Creates an DIV were the configuration option will be displyed.
 * It used DOJO to create the DIV background and also makes button
 * to close the DIV.
 */

	buttonImageA('configure', 'http://cnviewer.googlecode.com/hg/Images/chartOptions.png', graphConfigBox, 0, 50, 143, 210, 0, 0, 'Display configuration menu').style.display='none';

	var w1=1.5, h1=1;
	var w = w1*300, h= 2.5*250;
	var configDiv = document.createElement('div');
	configDiv.id='configDiv';
	configDiv.style.width=w+'px';
	configDiv.style.height=h+'px';
	configDiv.style.zIndex="401";
	setPosition(configDiv, 275,180);
	document.body.appendChild(configDiv);
	
	
	if (document.doctype){
		var surface = dojox.gfx.createSurface(configDiv, w, h);
	
		var rect1 = {
			x: 0,
			y: 0,
			width: w*w1,
			height: h*h1,
			r: 10
		};
		var lg1 = {
			type: "linear",
			x1: 0,
			y1: 0,
			x2: 0,
			y2: 200*h1,
			x3: 70*w1,
			y3: 300*h1,
			x4: 300*w1,
			y4: 400*h1,
			x5: 500*w1,
			y5: 300*h1,
			
			colors: [{
				offset: 0,
				color: "rgba(255,255,255,0)"
			}, 
			{
				offset: 0.1,
				color: "#b3ce7c"
			},
			{
				offset: 0.6,
				color: "#b1cc7a"
			},
			{
				offset: 0.9,
				color: "#dde879"
			},
					{
				offset: 1,
				color: "rgba(255,255,255,0)"
			}]
		};
			
			
		var vDetail = surface.createRect(rect1).setFill(lg1).setTransform({
			dx: 3,
			dy: 0
		});
		vDetail.getEventSource().setAttribute('id','configVRectDetail');
	}
	else{ //April 2011
		var configBg= document.createElement("img");
		configBg.src = "Images/bgMenu.png";
		configBg.id = "bgConfigMenu";
		configDiv.appendChild(configBg);
		configBg.width=w;
		configBg.height=220;
	}

	var close=buttonImageA('closeConfigForm', 'http://cnviewer.googlecode.com/hg/Images/close2.png', closeConfigBox, 20, 0, w-25, 18+15, 0, configDiv, 'Close');
	close.style.zIndex=100;
	
	return configDiv;
}


function createConfigForm(){
/* Formulary containing all graph and data configuration options */
	var configForm = document.createElement('FORM');
	configForm.id='configForm';
	setPosition(configForm, 15,1);
	configForm.style.zIndex="403";
	document.getElementById('configDiv').appendChild(configForm);
		
	var chartTitle = document.createElement('INPUT');
	chartTitle.id='chartTitle';
	chartTitle.type='text';
	chartTitle.value = document.getElementById('CNV').analysisTitle;

	if (!chartTitle.value.match(/segmented CNV data/g)){
		chartTitle.value = chartTitle.value +  " segmented CNV data";
	}
	chartTitle.onkeyup = updateChartTitle;
	chartTitle.onmouseout = updateChartTitle;
	chartTitle.style.width="260px";
	setFont(chartTitle,'15px',0,0,0);
	configForm.appendChild(chartTitle);
	setPosition(chartTitle, 140, 90, 'absolute');
	
	var chartTitleLabel=createParagraph('chartTitleLabel', 'Chart Title', configForm);
	chartTitleLabel.style.width="115px";
	setFont(chartTitleLabel,'15px',0,'right',0);
	configForm.appendChild(chartTitleLabel);
	setPosition(chartTitleLabel, 15, 90, 'absolute');
	
	var segHeight = document.createElement('INPUT');
	segHeight.id='segHeight';
	segHeight.type='text';
	segHeight.value = document.getElementById('graphicVariables').segmentHeight;
	segHeight.onchange = updateSegHeight;
	segHeight.style.width="30px";
	//segHeight.style.height="50px";
	setFont(segHeight,'15px',0,0,0);
	configForm.appendChild(segHeight);
	setPosition(segHeight, 140, 127.5, 'absolute');

	var segHeightLabel=createParagraph('segHeightLabel', 'Segment\nthickness ', configForm);
	segHeightLabel.style.width="115px";
	setFont(segHeightLabel,'15px',0,'right',0);
	configForm.appendChild(segHeightLabel);
	setPosition(segHeightLabel, 15, 122.5, 'absolute');
		
	return false;
}

function updateChartTitle(){
	document.getElementById('CNV').analysisTitle = this.value;//document.getElementById('chartTitle').value;
	if (document.getElementById("plotP")){
		document.getElementById("plotP").innerHTML=this.value;
	}
	return false;
}

function updateSegHeight(){
	if (checkSegHeight()) {
		document.getElementById('graphicVariables').segmentHeight = document.getElementById('segHeight').value;
	}
	else{
		alert('The segment height must be numeric only.\nPlease verify your entry.');
	}
	return false;
}

function checkSegHeight(){
/* Verifies if the number typed by the user is compatible */
	var patt=/[^0-9]/;
	if (document.getElementById('segHeight').value.match(patt)){
		return 0;
	}
	else{
		return 1;
	}
}

function verifySegPlotted(){
/* Verifies an deal with the typed number*/

	switch(checkSegPlotted()) {
		case 0:
			break;
		case 1:
			alert ('The number of segments to be displayed must be positive, equal or smaller then '+ this.totalSamples + '.');	
			break;
		case 2:
			alert('The number of segments to be displayed must be numeric only.\nPlease verify your entry.');
	}
	return false;
}

function checkSegPlotted(){
/* Verifies if the number typed by the user is compatible */
	var patt=/[^0-9]/g;
	var entry = document.getElementById('segPlotted').value;
	if (entry.match(patt)) {
			if (entry.match(patt) == "-") {
				return 1;
			}
			else{
				return 0;
			}
	}
	else{
		if (entry > document.getElementById('segPlotted').totalSamples) {
			return 1;
		}
		else {
			return 0;
		}
	}
	return false;
}

function closeConfigBox(){
/* Hides the Configuration Box and makes the configuration button visible */
	if (!checkSegHeight()){
		document.getElementById('segHeight').value = document.getElementById('graphicVariables').segmentHeight;
	}
	document.getElementById('configDiv').style.display='none';
	document.getElementById('configure').style.display = '';
	document.getElementById('configure').src='http://cnviewer.googlecode.com/hg/Images/chartOptions.png';
	
	return false;
}

function graphConfigBox(){
/* Show Configuration Box and hides its button */
	if (document.getElementById('configDiv').style.display=='none') {
		document.getElementById('configure').src='http://cnviewer.googlecode.com/hg/Images/chartOptions_.png';
		document.getElementById('configDiv').style.display = '';
	}
	else{
		document.getElementById('configure').src='http://cnviewer.googlecode.com/hg/Images/chartOptions.png';
		document.getElementById('configDiv').style.display = 'none';
	}
	return false;
}
